package com.chat.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FileUtils extends org.apache.commons.io.FileUtils {
	private static Logger log = LoggerFactory.getLogger(FileUtils.class);
	public static boolean copyFile(String srcFileName, String descFileName) {
		return FileUtils.copyFileCover(srcFileName, descFileName, false);
	}
	public static boolean copyFileCover(String srcFileName,
			String descFileName, boolean coverlay) {
		File srcFile = new File(srcFileName);
		if (!srcFile.exists()) {
			return false;
		}
		else if (!srcFile.isFile()) {
			return false;
		}
		File descFile = new File(descFileName);
		if (descFile.exists()) {
			if (coverlay) {
				if (!FileUtils.delFile(descFileName)) {
					return false;
				}
			} else {
				return false;
			}
		} else {
			if (!descFile.getParentFile().exists()) {
				if (!descFile.getParentFile().mkdirs()) {
					return false;
				}
			}
		}
		int readByte = 0;
		InputStream ins = null;
		OutputStream outs = null;
		try {
			ins = new FileInputStream(srcFile);
			outs = new FileOutputStream(descFile);
			byte[] buf = new byte[1024];
			while ((readByte = ins.read(buf)) != -1) {
				outs.write(buf, 0, readByte);
			}
			return true;
		} catch (Exception e) {
			return false;
		} finally {
			if (outs != null) {
				try {
					outs.close();
				} catch (IOException oute) {
					oute.printStackTrace();
				}
			}
			if (ins != null) {
				try {
					ins.close();
				} catch (IOException ine) {
					ine.printStackTrace();
				}
			}
		}
	}
	public static boolean copyDirectory(String srcDirName, String descDirName) {
		return FileUtils.copyDirectoryCover(srcDirName, descDirName,
				false);
	}
	public static boolean copyDirectoryCover(String srcDirName,
			String descDirName, boolean coverlay) {
		File srcDir = new File(srcDirName);
		if (!srcDir.exists()) {
			return false;
		}
		else if (!srcDir.isDirectory()) {
			return false;
		}
		String descDirNames = descDirName;
		if (!descDirNames.endsWith(File.separator)) {
			descDirNames = descDirNames + File.separator;
		}
		File descDir = new File(descDirNames);
		if (descDir.exists()) {
			if (coverlay) {
				if (!FileUtils.delFile(descDirNames)) {
					return false;
				}
			} else {
				return false;
			}
		} else {
			if (!descDir.mkdirs()) {
				return false;
			}
		}
		boolean flag = true;
		File[] files = srcDir.listFiles();
		for (int i = 0; i < files.length; i++) {
			if (files[i].isFile()) {
				flag = FileUtils.copyFile(files[i].getAbsolutePath(),
						descDirName + files[i].getName());
				if (!flag) {
					break;
				}
			}
			if (files[i].isDirectory()) {
				flag = FileUtils.copyDirectory(files[i]
						.getAbsolutePath(), descDirName + files[i].getName());
				if (!flag) {
					break;
				}
			}
		}
		if (!flag) {
			return false;
		}
		return true;
	}
	public static boolean delFile(String fileName) {
 		File file = new File(fileName);
		if (!file.exists()) {
			return true;
		} else {
			if (file.isFile()) {
				return FileUtils.deleteFile(fileName);
			} else {
				return FileUtils.deleteDirectory(fileName);
			}
		}
	}
	public static boolean deleteFile(String fileName) {
		File file = new File(fileName);
		if (file.exists() && file.isFile()) {
			if (file.delete()) {
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	}
	public static boolean deleteDirectory(String dirName) {
		String dirNames = dirName;
		if (!dirNames.endsWith(File.separator)) {
			dirNames = dirNames + File.separator;
		}
		File dirFile = new File(dirNames);
		if (!dirFile.exists() || !dirFile.isDirectory()) {
			return true;
		}
		boolean flag = true;
		File[] files = dirFile.listFiles();
		for (int i = 0; i < files.length; i++) {
			if (files[i].isFile()) {
				flag = FileUtils.deleteFile(files[i].getAbsolutePath());
				if (!flag) {
					break;
				}
			}
			else if (files[i].isDirectory()) {
				flag = FileUtils.deleteDirectory(files[i]
						.getAbsolutePath());
				if (!flag) {
					break;
				}
			}
		}
		if (!flag) {
			return false;
		}
		if (dirFile.delete()) {
			return true;
		} else {
			return false;
		}

	}
	public static boolean createFile(String descFileName) {
		File file = new File(descFileName);
		if (file.exists()) {
			return false;
		}
		if (descFileName.endsWith(File.separator)) {
			return false;
		}
		if (!file.getParentFile().exists()) {
			if (!file.getParentFile().mkdirs()) {
				return false;
			}
		}
		try {
			if (file.createNewFile()) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}
	public static boolean createDirectory(String descDirName) {
		String descDirNames = descDirName;
		if (!descDirNames.endsWith(File.separator)) {
			descDirNames = descDirNames + File.separator;
		}
		File descDir = new File(descDirNames);
		if (descDir.exists()) {
			return false;
		}
		if (descDir.mkdirs()) {
			return true;
		} else {
			return false;
		}

	}
	public static void writeToFile(String fileName, String content, boolean append) {
		try {
			FileUtils.write(new File(fileName), content, "utf-8", append);
		} catch (IOException e) {
		}
	}
	public static void writeToFile(String fileName, String content, String encoding, boolean append) {
		try {
			FileUtils.write(new File(fileName), content, encoding, append);
		} catch (IOException e) {
		}
	}
	public static void zipFiles(String srcDirName, String fileName,
			String descFileName) {
		if (srcDirName == null) {
			return;
		}
		File fileDir = new File(srcDirName);
		if (!fileDir.exists() || !fileDir.isDirectory()) {
			return;
		}
		String dirPath = fileDir.getAbsolutePath();
		File descFile = new File(descFileName);
		try {
			ZipOutputStream zouts = new ZipOutputStream(new FileOutputStream(
					descFile));
			if ("*".equals(fileName) || "".equals(fileName)) {
				FileUtils.zipDirectoryToZipFile(dirPath, fileDir, zouts);
			} else {
				File file = new File(fileDir, fileName);
				if (file.isFile()) {
					FileUtils.zipFilesToZipFile(dirPath, file, zouts);
				} else {
					FileUtils
							.zipDirectoryToZipFile(dirPath, file, zouts);
				}
			}
			zouts.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	public static void zipDirectoryToZipFile(String dirPath, File fileDir,
			ZipOutputStream zouts) {
		if (fileDir.isDirectory()) {
			File[] files = fileDir.listFiles();
			if (files.length == 0) {
				ZipEntry entry = new ZipEntry(getEntryName(dirPath, fileDir));
				try {
					zouts.putNextEntry(entry);
					zouts.closeEntry();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return;
			}
			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					FileUtils
							.zipFilesToZipFile(dirPath, files[i], zouts);
				} else {
					FileUtils.zipDirectoryToZipFile(dirPath, files[i],
							zouts);
				}
			}
		}
	}
	public static void zipFilesToZipFile(String dirPath, File file,
			ZipOutputStream zouts) {
		FileInputStream fin = null;
		ZipEntry entry = null;
		byte[] buf = new byte[4096];
		int readByte = 0;
		if (file.isFile()) {
			try {
				fin = new FileInputStream(file);
				entry = new ZipEntry(getEntryName(dirPath, file));
				zouts.putNextEntry(entry);
				while ((readByte = fin.read(buf)) != -1) {
					zouts.write(buf, 0, readByte);
				}
				zouts.closeEntry();
				fin.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
	private static String getEntryName(String dirPath, File file) {
		String dirPaths = dirPath;
		if (!dirPaths.endsWith(File.separator)) {
			dirPaths = dirPaths + File.separator;
		}
		String filePath = file.getAbsolutePath();
		if (file.isDirectory()) {
			filePath += "/";
		}
		int index = filePath.indexOf(dirPaths);
		return filePath.substring(index + dirPaths.length());
	}
	public static String contentToHtml(HttpServletRequest request, String contents) {
		String path = request.getSession().getServletContext().getRealPath(Global.HTMLFILES_BASE_URL);
		String name =  UUID.randomUUID() + ".html";
		String fileName = path+"\\"+name;
		FileUtils.writeToFile(fileName, contents,Global.DEFAULT_ENCODE, false);
		String url = Global.HTMLFILES_BASE_URL+name;
		return url;
	}
	public static void contentToHtml(HttpServletRequest request, String contents,String name) {
		String path = request.getSession().getServletContext().getRealPath("/");
		String fileName = path+name;
		FileUtils.writeToFile(fileName,contents,Global.DEFAULT_ENCODE, false);
	}
	public static String htmlToContent(HttpServletRequest request, String url) {
		String path = request.getSession().getServletContext().getRealPath("/");
		String fileName = path+url;
		File file = new File(fileName);
		String content = "";
		try {
			content = FileUtils.readFileToString(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return content;
	}
}
